﻿using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Platform.Storage;
using DX.IO;
using SkiaSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DX
{
    class DefaultFileSystemDialog : IFileSystemDialog
    {
        private readonly IClassicDesktopStyleApplicationLifetime applicationLifetime;

        public DefaultFileSystemDialog(IClassicDesktopStyleApplicationLifetime applicationLifetime)
        {
            this.applicationLifetime = applicationLifetime;
        }
        public async Task<FileEntry> OpenSingleFile(string filter)
        {
            var topLevel = TopLevel.GetTopLevel(applicationLifetime.MainWindow);
            var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
            {
                Title = "Open Text File",
                FileTypeFilter = GetFileTypes(filter),
                AllowMultiple = false
            });

            if (files.Count == 0)
            {
                return null;
            }
            var file = files[0];

            return new AvaloniaFileEntry(file);
        }
        FilePickerFileType[] GetFileTypes(string filter)
        {
            if (string.IsNullOrEmpty(filter)) return Array.Empty<FilePickerFileType>();
            var array = filter.Split('|');
            if (array.Length == 1) return new[] { new FilePickerFileType(filter) };
            IList<FilePickerFileType> list = new List<FilePickerFileType>();
            for (var i = 0; i < array.Length; i = i + 2)
            {
                FilePickerFileType item = new FilePickerFileType(array[i]);
                item.Patterns = array[i + 1].Split(';');
                list.Add(item);
            }
            return list.ToArray();
        }
        public async Task<FileEntry[]> OpenMultipleFile(string filter)
        {

            var topLevel = TopLevel.GetTopLevel(applicationLifetime.MainWindow);
            var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
            {
                Title = "Open Text File",
                FileTypeFilter = GetFileTypes(filter),
                AllowMultiple = true
            });

            if (files.Count == 0)
            {
                return Array.Empty<FileEntry>();
            }

            return files.Select(n => new AvaloniaFileEntry(n)).ToArray();

        }

        public async Task OpenSaveAs(FileEntry file, string filter = "")
        {
            var topLevel = TopLevel.GetTopLevel(applicationLifetime.MainWindow);
            var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
            {
                Title = "Open Text File",
                FileTypeFilter = GetFileTypes(filter),
                AllowMultiple = false
            });

            if (files.Count == 0)
            {
                return;
            }
            var item = files.First();
            var stream = await item.OpenWriteAsync();
            var buffer = file.GetBuffer();
            await stream.WriteAsync(buffer);
            await stream.FlushAsync();
        }
    }
}
